import { AchiveItemId } from '../define/GameDefine';

const ItemData = {
  [AchiveItemId.Apple]: {
    id: AchiveItemId.Apple,
    icon: 'items/apple',
    name: '苹果',
    description: '苹果，一种水果',
    winText: '一日一苹果，疾病远离我！',
  },
  [AchiveItemId.Medic]: {
    id: AchiveItemId.Medic,
    icon: 'items/capsule',
    name: '💊',
    description: '这是一粒治病的药',
    winText: '有病是要治的！',
  },
  [AchiveItemId.YaLing]: {
    id: AchiveItemId.YaLing,
    icon: 'items/yaling',
    name: '哑铃',
    description: '哑铃，一种健身器材',
    winText: '在忙也要坚持运动哦！',
  },
  [AchiveItemId.YanZhao]: {
    id: AchiveItemId.YanZhao,
    icon: 'items/yanzhao',
    name: '眼罩',
    description: '眼罩，让你睡个安稳觉',
    winText: '充足睡眠才能拥有一天好心情！',
  },
  [AchiveItemId.Card]: {
    id: AchiveItemId.Card,
    icon: 'items/worker',
    name: '门禁卡',
    description: '门禁卡，可以"自由"进出写字楼',
    winText: '写字楼出入通行证！',
  },
  [AchiveItemId.Clock]: {
    id: AchiveItemId.Clock,
    icon: 'items/clock',
    name: '闹钟',
    description: '一只会😱的闹钟',
    winText: '专治打工人的起床困难症！',
  },
  [AchiveItemId.Offer]: {
    id: AchiveItemId.Offer,
    icon: 'items/offer',
    name: 'Offer',
    description: '心仪公司生成的 Offer',
    winText: '令人心动的offer，开启职场生涯！',
  },
  [AchiveItemId.MacBook]: {
    id: AchiveItemId.MacBook,
    icon: 'items/computer',
    name: '💻',
    description: '笔记本电脑，你可以用来打游戏，也可以用来 coding',
    winText: '吃饭的家伙！',
  },
  [AchiveItemId.Heart]: {
    id: AchiveItemId.Heart,
    icon: 'items/heart',
    name: '❤️',
    description: 'Heart',
    winText: '善良的人总是会拥有好运！',
  },
  [AchiveItemId.Steal]: {
    id: AchiveItemId.Steal,
    icon: 'items/steal',
    name: '牵羊手套',
    description: '可以随机偷取其他人包裹里的道具',
  },
};
export class Item {
  public id: number;
  public name: string;
  public description: string;
  public icon: string;
  public winText: string;

  public spriteFramePromise: Promise<cc.SpriteFrame>;
  public spriteFrame: cc.SpriteFrame;

  public constructor(id: number) {
    this.id = id;
    const data = ItemData[id];
    if (!data) {
      console.log(`cannot find item id: ${id}`);
    }
    this.name = data.name;
    this.description = data.description;
    this.icon = data.icon;
    this.winText = data.winText;

    this.spriteFramePromise = new Promise((resolve, reject) => {
      cc.loader.loadRes(this.icon, cc.SpriteFrame, function (err, spriteFrame) {
        if (err) {
          reject(err);
        } else {
          resolve(spriteFrame);
        }
      });
    });
    this.spriteFramePromise.then((res) => {
      this.spriteFrame = res;
      this.spriteFramePromise = null;
    });
  }

  public get canUse() {
    return this.id < 1000;
  }

  public get isSuccessType() {
    return this.id >= 1000;
  }
}
